package contents;

import contents.entities.DateSubstitutions;
import contents.entities.Subject;
import contents.entities.SubstitutionItem;
import contents.entities.Teacher;
import contents.entities.NoticeboardNotice;
import contents.entities.SubjectGrades;
import contents.entities.Tutorage;
import contents.entities.TutoragesForTutorage;
import contents.entities.Student;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import database.Database;
import database.DatabaseEntities;
import java.sql.SQLException;
import java.util.Collections;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class StudentContent {

    public static HttpServletRequest setContent(HttpServletRequest request) throws SQLException {
        String contentRequest = (String) request.getParameter("content");
        
        if (contentRequest == null) {
            // do nothing
        } else if (contentRequest.equals("noticeboard")) {
            request = setNoticeboardContent(request);
        } else if (contentRequest.equals("substitutions")) {
            request = setSubstitutionsContent(request);
        } else if (contentRequest.equals("gradebook")) {
            request = setGradebookContent(request);
        } else if (contentRequest.equals("myTutorages")) {
            request = setMyTutoragesContent(request);
        } else if (contentRequest.equals("allTutorages")) {
            request = setAllTutoragesContent(request);
        } else if (contentRequest.equals("deleteTutorage")) {
            deleteTutorage(request);
        } else if (contentRequest.equals("addTutorage")) {
            addTutorage(request);
        }
        
        return request;
    }
    
    private static HttpServletRequest setNoticeboardContent(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int studentId = (Integer) session.getAttribute("userId");
        int classId = Database.selectClassId(studentId);
        Map<Integer, NoticeboardNotice> notices = Database.selectNoticesForStudent(classId);
        request.setAttribute("notices", notices.values());       
        
        return request;
    }
    
    private static HttpServletRequest setSubstitutionsContent(HttpServletRequest request) throws SQLException {
        Map<Integer, DateSubstitutions> dateSubstitutions = new HashMap<Integer, DateSubstitutions>();
        
        HttpSession session = request.getSession();
        int studentId = ((Integer) session.getAttribute("userId"));
        int classId = Database.selectClassId(studentId);
        
        // get next monday date
        Date mondayDate = common.CommonMethods.getMondayDate();

        Date currentDate;
        for (int i = 0; i < 5; i++) {
            currentDate = new Date(mondayDate.getTime());
            currentDate.setDate(currentDate.getDate() + i);

            // get substitutions of the day and of student's class
            List<DatabaseEntities.Substitution> substitutions = Database.selectSubstitutionsOfDateAndClass(currentDate, classId);
            
            List<SubstitutionItem> substitutionItems = new ArrayList<SubstitutionItem>();
            
            for (int j = 0; j < substitutions.size(); j++) {
                DatabaseEntities.Substitution substitution = substitutions.get(j);
                
                String className = Database.selectClassName(substitution.classId);
                Integer lesson = Integer.valueOf(substitution.unit);
                Subject subject = Database.getSubject(substitution.subjectId);
                Teacher missingTeacher = Database.getTeacher(substitution.missingTeacherId);
                
                Teacher substitutingTeacher;
                if (substitution.substitutingTeacherId == null) {
                    substitutingTeacher = null;
                } else {
                    substitutingTeacher = Database.getTeacher(substitution.substitutingTeacherId.intValue());
                }
            
                substitutionItems.add(new SubstitutionItem(className, lesson, subject, missingTeacher, substitutingTeacher));
            }
            Collections.sort(substitutionItems, new SubstitutionItem.ByClassNameAndTimeComparator());
            
            dateSubstitutions.put(i, new DateSubstitutions(currentDate, substitutionItems));
        }
        
        request.setAttribute("substitutionsList", dateSubstitutions.values());
        return request;
    }
    
    

    private static HttpServletRequest setGradebookContent(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int studentId = (Integer) session.getAttribute("userId");
        Map<Integer, SubjectGrades> grades = Database.selectGradesForStudent(studentId);
        request.setAttribute("grades", grades.values());       
        
        return request;
    }
        
    private static HttpServletRequest setMyTutoragesContent(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int studentId = ((Integer) session.getAttribute("userId")).intValue();
        
        List<DatabaseEntities.Tutorage> myDatabaseTutorages = Database.selectTutoragesOfStudent(studentId);
        
        List<TutoragesForTutorage> myTutoragesListSorted = new ArrayList<TutoragesForTutorage>();
        for (int i = 0; i < myDatabaseTutorages.size(); i++) {
            int myTutorageId = myDatabaseTutorages.get(i).tutorageId;
            Subject myTutorageSubject = Database.getSubject(myDatabaseTutorages.get(i).subjectId);
            Student myTutorageStudent = Database.getStudent(myDatabaseTutorages.get(i).studentId);
            
            String myTutorageType;
            if (myDatabaseTutorages.get(i).type.equals("O")) {
                myTutorageType = "tutorage_offer";
            } else {
                myTutorageType = "tutorage_search";
            }
            
            String myTutorageClassName = myDatabaseTutorages.get(i).className;
            String myTutorageDateString = myDatabaseTutorages.get(i).dateString;
            String myTutorageTimeString = myDatabaseTutorages.get(i).timeString;
            
            Tutorage myTutorage = new Tutorage(myTutorageId, myTutorageSubject, myTutorageStudent, myTutorageType, myTutorageClassName, 
                    myTutorageDateString, myTutorageTimeString);
            
            List<DatabaseEntities.Tutorage> remotePeersDatabaseTutorages = Database.selectRemoteTutoragesForSubjectWithoutThoseOfStudent(myTutorageSubject.getSubjectId(), 
                    studentId, myDatabaseTutorages.get(i).type);
            
            List<Tutorage> remotePeersTutoragesListSorted = new ArrayList<Tutorage>();
            for (int j = 0; j < remotePeersDatabaseTutorages.size(); j++) {
                int tutorageId = remotePeersDatabaseTutorages.get(j).tutorageId;
                Subject subject = Database.getSubject(remotePeersDatabaseTutorages.get(j).subjectId);
                Student student = Database.getStudent(remotePeersDatabaseTutorages.get(j).studentId);
            
                String type;
                if (remotePeersDatabaseTutorages.get(j).type.equals("O")) {
                    type = "tutorage_offer";
                } else {
                    type = "tutorage_search";
                }
            
                String className = remotePeersDatabaseTutorages.get(j).className;
                String dateString = remotePeersDatabaseTutorages.get(j).dateString;
                String timeString = remotePeersDatabaseTutorages.get(j).timeString;
                        
                remotePeersTutoragesListSorted.add(new Tutorage(tutorageId, subject, student, type, className, dateString, timeString));
            }
            Collections.sort(remotePeersTutoragesListSorted, new Tutorage.ByDateComparator());   
                        
            myTutoragesListSorted.add(new TutoragesForTutorage(myTutorage, remotePeersTutoragesListSorted));
        }
        
        Collections.sort(myTutoragesListSorted, new TutoragesForTutorage.ByDateComparator());
        
        Map<Integer, TutoragesForTutorage> myTutorages = new HashMap<Integer, TutoragesForTutorage>(); 
        for (int i = 0; i < myTutoragesListSorted.size(); i++) {
            myTutorages.put(i+1, myTutoragesListSorted.get(i));
        }
        request.setAttribute("tutorages", myTutorages.values());
        
        Map<Integer, Subject> subjects = Database.selectSubjects();
        request.setAttribute("subjects", subjects.values());
        
        return request;
    }
        
    private static HttpServletRequest setAllTutoragesContent(HttpServletRequest request) throws SQLException {
        List<DatabaseEntities.Tutorage> databaseTutorages = Database.selectAllTutorages();
        
        List<Tutorage> tutoragesListSorted = new ArrayList<Tutorage>();

        for (int i = 0; i < databaseTutorages.size(); i++) {
            int tutorageId = databaseTutorages.get(i).tutorageId;
            Subject subject = Database.getSubject(databaseTutorages.get(i).subjectId);
            Student student = Database.getStudent(databaseTutorages.get(i).studentId);
            
            String type;
            if (databaseTutorages.get(i).type.equals("O")) {
                type = "tutorage_offer";
            } else {
                type = "tutorage_search";
            }
            
            String className = databaseTutorages.get(i).className;
            String dateString = databaseTutorages.get(i).dateString;
            String timeString = databaseTutorages.get(i).timeString;
                        
            tutoragesListSorted.add(new Tutorage(tutorageId, subject, student, type, className, dateString, timeString));
        }
        
        Collections.sort(tutoragesListSorted, new Tutorage.ByDateComparator());
        
        Map<Integer, Tutorage> tutorages = new HashMap<Integer, Tutorage>(); 
        for (int i = 0; i < tutoragesListSorted.size(); i++) {
            tutorages.put(i+1, tutoragesListSorted.get(i));
        }
        
        request.setAttribute("tutorages", tutorages.values());
        return request;
    }
    
    private static void deleteTutorage(HttpServletRequest request) throws SQLException {
        int tutorageId = Integer.valueOf(request.getParameter("tutorageId")).intValue();
        
        HttpSession session = request.getSession();
        int studentId = ((Integer) session.getAttribute("userId")).intValue();
        
        Database.deleteTutorage(tutorageId, studentId);
    }
    
    private static void addTutorage(HttpServletRequest request) throws SQLException {
        HttpSession session = request.getSession();
        int studentId = ((Integer) session.getAttribute("userId")).intValue();
        
        int subjectId = Integer.valueOf(request.getParameter("subjectId"));
        String tutorageType = request.getParameter("tutorageType");
        
        Database.addTutorage(studentId, subjectId, tutorageType);
    }
    
    private StudentContent() {
    }
}
